<template>
  <div>
    <!-- 卡片试图 -->
    <el-card>
      <!-- 输入框 -->
      <el-row>
        <el-col :span="5">
          <div class="grid-content bg-purple">
            <el-input v-model="usersName" size="small" placeholder="根据用户名搜索" style="width:200px"></el-input>
          </div>
        </el-col>
        <el-col :span="10">
          <el-button @click="clearInput" size="small">清空</el-button>
          <!-- 搜索按钮 -->
          <el-button type="primary" size="small" @click="searchUser">搜索</el-button>
        </el-col>
        <el-col :span="9" style="text-align:right">
          <el-button type="success" size="small" icon="el-icon-edit" @click="showDialog">新增用户
          </el-button>
        </el-col>
      </el-row>
      <!-- 警示框 -->
      <el-alert show-icon :title="'共'+total+'条记录'" type="info" :closable="false">
      </el-alert>
      <!-- 表格 -->
      <el-table style="width: 100%" stripe :data="userList">
        <el-table-column prop="id" label="序号" align="center">
        </el-table-column>
        <el-table-column label="邮箱" prop="email" align="center">
        </el-table-column>
        <el-table-column label="联系电话" prop="phone" align="center">
        </el-table-column>
        <el-table-column label="用户名" prop="username" align="center">
        </el-table-column>
        <el-table-column label="权限组名称" prop="permission_group_title" align="center">
        </el-table-column>
        <el-table-column label="角色" prop="role" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template v-slot="{row}">
            <el-button icon="el-icon-edit" type="primary" plain circle @click="showEditDialog(row)">
            </el-button>
            <el-button icon="el-icon-delete" type="danger" plain circle v-if="row.id===2?false:true"
              @click="clearUsers(row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.page" :page-sizes="[10, 20, 30, 50]" :page-size="queryInfo.pagesize"
        layout=" prev, pager,next, sizes,jumper" :total="total">
      </el-pagination>
    </el-card>

    <!-- 新增用户对话框 -->
    <el-dialog title="创建用户" :visible.sync="addDialog" width="50%" @close="closeAddForm">
      <el-form label-position="right" label-width="100px" :model="addForm" :rules="addFormRules" ref="addFormRef"
        style="width: 400px; margin-left: 120px;">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="角色" v-model="addForm.role">
          <el-input v-model="addForm.role"></el-input>
        </el-form-item>
        <el-form-item label="权限组名称">
          <el-select v-model="addForm.permission_group_id" placeholder="请选择">
            <el-option v-for="obj in PermissionList " :key="obj.id" :label="obj.title" :value="obj.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="介绍">
          <el-input placeholder="please input" type="textarea" v-model="addForm.introduction"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click=" addDialog= false">取消</el-button>
        <el-button type="primary" @click="addPermission">确定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑用户对话框 -->
    <el-dialog title="编辑用户" :visible.sync="editDialog" width="50%" @close=closeEditForm>
      <el-form label-position="right" label-width="100px" :model="editForm" :rules="addFormRules" ref="editFormRef"
        style="width: 400px; margin-left: 120px;">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="editForm.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email"></el-input>
        </el-form-item>
        <el-form-item label="角色" v-model="editForm.role">
          <el-input v-model="editForm.role"></el-input>
        </el-form-item>
        <el-form-item label="权限组名称">
          <el-select v-model="editForm.permission_group_id" placeholder="请选择">
            <el-option v-for="obj in PermissionList " :key="obj.id" :label="obj.title" :value="obj.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="editForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="介绍">
          <el-input type="textarea" placeholder="please input" v-model="editForm.introduction"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click=" editDialog= false">取消</el-button>
        <el-button type="primary" @click="editPermission">确定</el-button>
      </span>
    </el-dialog>

  </div>

</template>

<script>
// 引用
import { list, add, remove, detail, update } from '@/api/base/users.js'
import { simple } from '@/api/base/permissions.js'

export default {

  data () {
    return {
      editForm: {},
      editDialog: false,
      addFormRules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      },

      addForm: {
        avatar: '',
        email: '',
        introduction: '',
        password: '',
        permission_group_id: '',
        phone: '',
        role: '',
        username: ''
      },
      PermissionList: [],
      addDialog: false,
      userList: [],
      total: 0,
      usersName: '',
      queryInfo: {
        page: 1,
        pagesize: 10,
        username: ''
      }
    }
  },
  // 获取用户信息
  created () {
    this.getUsersList()
  },

  methods: {
    // 获取用户列表
    async getUsersList () {
      const res = await list(this.queryInfo)
      if (res.status !== 200) return this.$message.error('获取用户数据失败')
      this.userList = res.data.list
      this.total = res.data.counts
    },
    // 点击搜索的函数
    async searchUser () {
      const res = await list({
        page: 1,
        pagesize: 20,
        username: this.usersName
      })
      if (res.status !== 200) return this.$message.error('获取用户数据失败')
      this.userList = res.data.list
      this.total = res.data.counts
    },
    // 点击清空的函数
    clearInput () {
      this.usersName = ''
      this.getUsersList()
    },

    // 点击显示添加对话框
    async showDialog () {
      this.addDialog = true
      const res = await simple({ username: '' })
      if (res.status !== 200) return this.$message.error('获取权限数据失败')

      this.PermissionList = res.data
    },

    // 点击确定发送添加请求
    addPermission () {
      this.$refs.addFormRef.validate(async (valid) => {
        if (valid) {
          const res = await add(this.addForm)
          if (res.status !== 200) return this.$message.error('添加用户失败')
          this.$message.success('添加用户成功')
          this.getUsersList()
          this.addDialog = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    // 关闭时重置
    closeAddForm () {
      this.$refs.addFormRef.resetFields()
    },
    // 点击编辑
    async showEditDialog (row) {
      this.editDialog = true
      const res = await detail(row)
      this.editForm = res.data
      console.log(res)
    },
    // 点击确定修改数据
    async editPermission () {
      this.$refs.editFormRef.validate(async (valid) => {
        if (valid) {
          const res = await update(this.editForm)
          if (res.status !== 200) return this.$message.error('修改用户失败')
          this.$message.success('修改用户成功')
          console.log(res)
          this.getUsersList()
          this.editDialog = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 关闭时重置
    closeEditForm () {
      this.$refs.editFormRef.resetFields()
    },
    // 点击删除按钮
    clearUsers (row) {
      this.$confirm('此操作将永久删除用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await remove(row)

        if (res.status !== 200) return this.$message.error('删除失败')
        this.$message.success('删除成功')
        this.getUsersList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作!'
        })
      })
    },
    // 分页
    handleSizeChange (newsize) {
      this.queryInfo.pagesize = newsize
      this.getUsersList()
    },
    handleCurrentChange (newnum) {
      this.queryInfo.page = newnum
      this.getUsersList()
    }
  },
  components: {

  }
}
</script>
<style scoped lang='less'>
// 样式
.el-alert {
  margin: 20px 0;
}
.el-pagination {
  display: block;
  text-align: center;
  float: right;
  margin: 20px 0;
}
.el-card {
  margin: 20px;
}
/deep/ .el-dialog__footer {
  text-align: center;
}
</style>
